<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" @submit.native.prevent>
      <el-form-item label="关键字" prop="search">
        <el-input v-model="queryParams.search" placeholder="UID、手机号码、昵称" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['member:manageGold:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" :data="list" stripe border>
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="180" />
      <el-table-column label="UID" prop="userId" align="center" class-name="small-padding fixed-width" />
      <el-table-column label="电话" prop="userPhone" align="center" width="120" />
      <el-table-column label="昵称" prop="userNickName" align="center" width="120" />
      <el-table-column label="可以提现开始时间" prop="takeStart" align="center" width="180" />
      <el-table-column label="创建人" prop="createName" align="center" width="100" />
      <el-table-column label="修改人" prop="updateName" align="center" width="100" />
      <el-table-column label="说明" prop="remark" align="center" min-width="120" />

      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['member:manageGold:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['member:manageGold:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-row>
          <el-form-item label="UID" prop="userId">
            <el-select v-model="search" filterable placeholder="输入UID,名字,电话号码" style="width:100%" :filter-method="query" @change="changeType($event)">
              <el-option v-for="item in userList" :key="item.value" :label="item.label" :value="item">
              </el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-form-item label="可以提现时间" prop="takeStart">
          <el-date-picker style="width:100%" v-model="form.takeStart" type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" />
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入说明" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserTakeControl, addUserTakeControl, editUserTakeControl, removeUserTakeControl, getVipList } from "@/api/vip/vip.js";

export default {
  name: "manageGold", //出金控制
  data() {
    return {
      // 列表数据
      list: [],

      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      search: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
      },

      // 表单参数
      form: {},
      userList: {},
      rules: {
        preId: [
          { required: true, message: "吉祥号不能为空" },
          { type: "number", message: "吉祥号必须为数字值" },
        ],
        phone: [
          { required: true, message: "电话不能为空" },
          { type: "number", message: "电话必须为数字值" },
          // { min: 11, max: 11, message: "电话号码长度不对" },
        ],
        nickname: [{ required: true, message: "昵称不能为空" }],
        inviteCode: [{ required: true, message: "推荐人不能为空" }],
        password: [{ required: true, message: "密码不能为空" }],
      },
    };
  },
  created() {
    this.getList();
    this.getScreenUserList();
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    }
  },
  methods: {
    getList() {
      this.loading = true;
      listUserTakeControl(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.list = response.data;
          this.total = response.total;
        }
        this.loading = false;
      });
    },

    getScreenUserList() {
      // if (!this.userList.length) {
      //   return;
      // }
      getVipList({ search: this.search }).then((response) => {
        if (response.code == 200) {
          this.userList = response.data;
          this.userList.forEach(item => {
            item.label = item.id + '--' + item.phone + '--' + item.nickname;
            item.value = item.id
          })
        }
      });
    },

    query(params) {
      this.search = params
      this.getScreenUserList();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },

    changeType(a) {
      this.form.userId = a.id;

    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.search = undefined;
      this.title = "新增";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "编辑";
      this.form = JSON.parse(JSON.stringify(row));
      this.search = this.form.userId;
      this.open = true;
    },

    /** 提交按钮 */
    submitForm(from) {
      this.$refs[from].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            editUserTakeControl(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("编辑成功");
              }
            }).finally(res => {
              this.open = false;
              this.loading = true;

              setTimeout(() => {
                this.getList();
              }, 1000);
            });
          } else {
            addUserTakeControl(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
              }
            }).finally(res => {
              this.loading = true;
              setTimeout(() => {
                this.getList()
              }, 1000);
            });
          }
        } else {
          return false;
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除为"' + row.id + '"的数据项？')
        .then(() => {
          removeUserTakeControl({ id: row.id }).then((response) => {
            if (response.code == 200) {
              this.$modal.msgSuccess("删除成功");
              this.open = false;
            }
          });
        }).finally(res => {
          this.loading = true;
          setTimeout(() => {
            this.getList()
          }, 1000);
        });
    },
  },
};
</script>
<style scoped>
</style>
